from pyglet.gl import *
import pyglet
from pyglet.window import *
from pyglet import image
import os

window = pyglet.window.Window(width=640, height=480, resizable=True)

boxcol = [ [1.0, 0.0, 0.0], # bright: red
			[1.0, 0.5, 0.0],	# orange
			[1.0, 1.0, 0.0],	# yellow
			[0.0, 1.0, 0.0],	# green
			[0.0, 1.0, 1.0],	# blue
			]

# Dark: red, orange, yellow, green ,blue
topcol =[ [0.5, 0.0, 0.0],
		[0.5, 0.25, 0.0],
		[0.5, 0.5, 0.0],
		[0.0, 0.5 ,0.0],
		[0.0, 0.5, 0.5]]
			

xrot=yrot=zrot=0.0	# rotate the cube
xrotspeed = 1.0
yrotspeed = 0.0

box = None # display list storage
top = None #display list storage

yloop = None # loop for y axis
xloop = None # loop for x axies

def build_lists():
  global box, top
  box = glGenLists(2)

  glNewList(box, GL_COMPILE)	# new compiled box display list

  # draw the box without the top (it will be store in display list
  # and will not appear on the screen)
  glBegin(GL_QUADS)
  # bottom face
  glTexCoord2f(1.0, 1.0);	glVertex3f(-1.0, -1.0, -1.0)
  glTexCoord2f(0.0, 1.0);	glVertex3f(1.0, -1.0, -1.0)
  glTexCoord2f(0.0, 0.0);	glVertex3f(1.0, -1.0, 1.0)
  glTexCoord2f(1.0, 0.0);	glVertex3f(-1.0, -1.0, 1.0)
  # front face
  glTexCoord2f(0.0, 0.0);	glVertex3f(-1.0, -1.0, 1.0)
  glTexCoord2f(1.0, 0.0);	glVertex3f(1.0, -1.0, 1.0)
  glTexCoord2f(1.0, 1.0);	glVertex3f(1.0, 1.0, 1.0)
  glTexCoord2f(0.0, 1.0);	glVertex3f(-1.0, 1.0, 1.0)
  # back face
  glTexCoord2f(1.0, 0.0);	glVertex3f(-1.0, -1.0, -1.0)
  glTexCoord2f(1.0, 1.0);	glVertex3f(-1.0, 1.0, -1.0)
  glTexCoord2f(0.0, 1.0);	glVertex3f(1.0, 1.0, -1.0)
  glTexCoord2f(0.0, 0.0);	glVertex3f(1.0, -1.0, -1.0)
  # right face
  glTexCoord2f(1.0, 0.0);	glVertex3f(1.0, -1.0, -1.0)
  glTexCoord2f(1.0, 1.0);	glVertex3f(1.0, 1.0, -1.0)
  glTexCoord2f(0.0, 1.0);	glVertex3f(1.0, 1.0, 1.0)
  glTexCoord2f(0.0, 0.0);	glVertex3f(1.0, -1.0, 1.0)
  # left face
  glTexCoord2f(0.0, 0.0);	glVertex3f(-1.0, -1.0, -1.0)
  glTexCoord2f(1.0, 0.0);	glVertex3f(-1.0, -1.0, 1.0)
  glTexCoord2f(1.0, 1.0);	glVertex3f(-1.0, 1.0, 1.0)
  glTexCoord2f(0.0, 1.0);	glVertex3f(-1.0, 1.0, -1.0)
  glEnd()

  glEndList()	# Done building the list

  top=box+1

  glNewList(top, GL_COMPILE)	# new compiled top display list
  # Top face
  glBegin(GL_QUADS)
  glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, 1.0, -1.0)
  glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, 1.0, 1.0)
  glTexCoord2f(1.0, 0.0); glVertex3f(1.0, 1.0, 1.0)
  glTexCoord2f(1.0, 1.0); glVertex3f(1.0, 1.0, -1.0)
  glEnd()
  glEndList()

def load_gl_textures():
	# load bitmaps and convert to textures
	global texture
	texture_file = os.path.join('data', 'cube.bmp')
	texture_surf = image.load(texture_file)
	texture = texture_surf.get_texture()
	glBindTexture(GL_TEXTURE_2D, texture.id)

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)


	
def init():
  	"""
	Pyglet oftentimes calls this setup()	
	"""
	glEnable(GL_TEXTURE_2D)
	load_gl_textures()

	build_lists()

  	glShadeModel(GL_SMOOTH)	# Enables smooth shading
	glClearColor(0.0, 0.0, 0.0, 0.0) #Black background
	
	glClearDepth(1.0)		# Depth buffer setup
	glEnable(GL_DEPTH_TEST)		# Enables depth testing
	glDepthFunc(GL_LEQUAL)		# The type of depth test to do

	glEnable(GL_LIGHT0) # quick and dirty lighting 
	# Andriy: I disable this for now since it makes cubes shade as they
	# spin around
	#glEnable(GL_LIGHTING)	# enable lighting
	glEnable(GL_COLOR_MATERIAL)	# enable coloring

	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)	# Really nice perspective calculations


@window.event
def on_draw():
  	# Here we do all the drawing
	glClear(GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT)

	# Select the texture
	glBindTexture(GL_TEXTURE_2D, texture.id)

	for yloop in range(6):
	  	for xloop in range(yloop):
			glLoadIdentity()	# reset our view
			glTranslatef( (1.4 + xloop*2.8 - yloop*1.4),
						(6.0 - yloop)*2.4 - 7.0,
						  -20.0)
			#glRotatef(xrot, 0.0, 1.0, 0.0)
			glRotatef(45.0 - (2.0*yloop) + xrot, 1.0, 0.0, 0.0) # tilt up/down
			glRotatef(45.0 + yrot, 0.0, 1.0, 0.0)	# spin left/right
			glColor3f(*boxcol[yloop-1]) # select a box color
			glCallList(box)	# draw the box

			glColor3f(*topcol[yloop-1])
			glCallList(top)	 # draw the top


	"""
	red = (1.0, 0.0, 0.0)
	green = (0.0, 1.0, 0.0)
	blue = (0.0, 0.0, 1.0)
	orange = (1.0, 0.5, 0.0)
	yellow = (1.0, 1.0, 0.0)
	violet=(1.0, 0.0, 1.0)
	"""

	return pyglet.event.EVENT_HANDLED
  	
@window.event
def on_key_press(symbol, modifiers):
  	global xrot, yrot, xrotspeed, yrotspeed
	if symbol == key.RETURN or symbol == key.ESCAPE:
		  exit()
	elif symbol == key.LEFT:
	  	if key.Q == modifiers:
	  		yrotspeed-=0.2
		else:
	  		yrotspeed-=1.2
	elif symbol == key.RIGHT:
		yrotspeed+=1.2
	elif symbol == key.UP:
	  	xrotspeed -= 1.2
	elif symbol == key.DOWN:
	  	xrotspeed += 1.2
	elif symbol == key.SPACE:
	  	# stop rotating
	  	xrotspeed = yrotspeed = 0
	


def update(dt):
  	global xrot, yrot, zrot
	xrot += xrotspeed
	yrot += yrotspeed
	#xrot += xrot
	#xrot = xrot + xrot
	"""
	zrot+=0.5
	yrot+=0.4
	zrot+=0.3
	"""

	# Spin the objects with a constant speed

pyglet.clock.schedule(update)

@window.event
def on_resize(width, height):
	if height==0:
		height = 1
	glViewport(0, 0, width, height)

	glMatrixMode(GL_PROJECTION)
	glLoadIdentity()

	# Calculate the aspect ratio of the window
	gluPerspective(45.0, 1.0*width/height, 0.1, 100.0)

	glMatrixMode(GL_MODELVIEW)
	glLoadIdentity()
	return pyglet.event.EVENT_HANDLED
	
init()	

pyglet.app.run()
